package com.yueke.gemini.controller.impl;

import com.yueke.gemini.common.ApiResult;
import com.yueke.gemini.controller.IExcelController;
import com.yueke.gemini.entity.excel.ExportInfo;
import com.yueke.gemini.entity.excel.ImportInfo;
import com.yueke.gemini.utils.ExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ExcelController implements IExcelController {

    /**
     *
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public ApiResult<String> upload(MultipartFile file) throws IOException {




        return null;
    }

    /**
     * 读取 Excel（允许多个 sheet）
     */
    @Override
    public ApiResult<Object> readExcelWithSheets(MultipartFile excel) {
        ApiResult<Object> apiResult = new ApiResult<>();
        apiResult.setData(ExcelUtil.readExcel(excel, new ImportInfo()));
        apiResult.setMessage("读取成功！");
        return apiResult;
    }

    /**
     * 读取 Excel（指定某个 sheet）
     */
    @Override
    public ApiResult<Object> readExcel(MultipartFile excel, int sheetNo,
                            @RequestParam(defaultValue = "1") int headLineNum) {
        ApiResult<Object> apiResult = new ApiResult<>();
        apiResult.setData(ExcelUtil.readExcel(excel, new ImportInfo(), sheetNo, headLineNum));
        apiResult.setMessage("读取成功!");
        return apiResult;
    }

    /**
     * 导出 Excel（一个 sheet）
     */
    @Override
    public void writeExcel(HttpServletResponse response) throws IOException {
        List<ExportInfo> list = getList();
        String fileName = "一个 Excel 文件";
        String sheetName = "第一个 sheet";

        ExcelUtil.writeExcel(response, list, fileName, sheetName, new ExportInfo());
    }

    /**
     * 导出 Excel（多个 sheet）
     */
    @Override
    public void writeExcelWithSheets(HttpServletResponse response) throws IOException {
        List<ExportInfo> list = getList();
        String fileName = "一个 Excel 文件";
        String sheetName1 = "第一个 sheet";
        String sheetName2 = "第二个 sheet";
        String sheetName3 = "第三个 sheet";

        ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName1, new ExportInfo())
                .write(list, sheetName2, new ExportInfo())
                .write(list, sheetName3, new ExportInfo())
                .finish();
    }

    /**
     * 封装数据
     * @return
     */
    private List<ExportInfo> getList() {
        List<ExportInfo> list = new ArrayList<>();
        ExportInfo model1 = new ExportInfo();
        model1.setName("howie");
        model1.setAge("19");
        model1.setAddress("123456789");
        model1.setEmail("123456789@gmail.com");
        list.add(model1);
        ExportInfo model2 = new ExportInfo();
        model2.setName("harry");
        model2.setAge("20");
        model2.setAddress("198752233");
        model2.setEmail("198752233@gmail.com");
        list.add(model2);
        return list;
    }
}
